#include "server.h"

#include <cstdio>

client::client(cpp_socket* sk,unsigned int nid):    cpp_socket( sk ),
                                                    id( nid ),
                                                    status(STATUS_NONE),
                                                    op(  false ){}
client::~client(){}

void client::setName(string str){
    name = str;
}
void client::setStatus(unsigned char st){
    status = st;
}

void client::setOp(bool o){
    op = o;
}
bool client::getOp(){
    return op;
}

unsigned int client::ID(){
    return id;
}
unsigned char client::getStatus(){
    return status;
}
string client::getName(){
    return name;
}

int client::sendMes(message* mes){
    return s_send((char*)mes->ctt().data(),mes->ctt().length(),0);
}
message* client::recvMes(unsigned short len){
    unsigned short rec;
    char* buffer = new char[len];
    if( (rec = s_recv(buffer,len,0)) == 0 ) return NULL;
    if( rec > len ) return NULL;
    buffer[rec] = '\0';
    return new message(buffer,this);
}

clientList::clientList():   id( 0 ){}
clientList::~clientList(){
    for(clientList::iterator i = begin(); i < end(); i++){
        delete (*i);
    }
}

client* clientList::createCli(cpp_socket* sk){
    id++;
    push_back(new client(sk,id));
    return back();
}
clientList::iterator clientList::findCli(unsigned int inid){
    for(clientList::iterator i = begin(); i < end(); i++){
        if( (*i)->ID() == inid ) return i;
    }
    return end();
}
clientList::iterator clientList::findCli(string* str){
    unsigned int len = str->length();
    for(clientList::iterator i = begin(); i < end(); i++){
        if( !strncmp((*i)->getName().c_str(),str->c_str(), len )) return i;
    }
    return end();
}
clientList::iterator clientList::findCli(char* str){
    unsigned int len = strlen(str);
    for(clientList::iterator i = begin(); i < end(); i++){
        if( !strncmp((*i)->getName().c_str(),str,len )) return i;
    }
    return end();
}
client* clientList::clFindCli(unsigned int inid){
    clientList::iterator i;
    if( (i = findCli(inid)) == end() ) return NULL;
    return *i;
}
client* clientList::clFindCli(string* str){
    clientList::iterator i;
    if( (i = findCli(str)) == end() ) return NULL;
    return *i;
}
client* clientList::clFindCli(char* str){
    clientList::iterator i;
    if( (i = findCli(str)) == end() ) return NULL;
    return *i;
}

clientList::iterator clientList::dropCli(unsigned int inid){
    clientList::iterator i = findCli( inid );
    return dropCli(i);
}
clientList::iterator clientList::dropCli(clientList::iterator i){
    delete *i;
    return erase(i);
}

unsigned int clientList::ID(){
    return id;
}

message::message(string str,client* scli, client* rcli):
    sender( scli ), receiver( rcli ), content( str ){}

void message::setSdr(client* s){
    sender = s;
}
void message::setRcv(client* r){
    receiver = r;
}
void message::setStr(string str){
    content = str;
}

client* message::sdr(){ return sender; }
client* message::rcv(){ return receiver; }
string message::ctt(){ return content; }

messagequeue::messagequeue(){}
messagequeue::~messagequeue(){
    flush();
}

void messagequeue::drop(){
    delete front();
    pop();
}
void messagequeue::flush(){
    while( draw() );
}
message* messagequeue::draw(){
    if( empty() ) return NULL;
    message* out = front();
    pop();
    return out;
}
